package com.scs.application.core.flow;

import com.scs.application.modules.sys.entity.Flow;
import com.scs.application.modules.sys.entity.FlowButton;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/**
 * @description: 流程上下文
 */
@Data
public class FlowContext {

    private Flow flow;

    private FlowButton button;

    private List<FlowButton> buttons;

    private String busId;
    private String busIdName;
    private String busTable;
    private String busTypeName;
    private String flowStatusTextTo;
    private String flowStatusTextFrom;
    private List<String> busIdList;


    @Setter(AccessLevel.NONE)
    private List<String> busIdErrorList;

    @Setter(AccessLevel.NONE)
    private List<String> busKeyErrorList;
    @Setter(AccessLevel.NONE)
    private List<String> busErrorMsgList;

    // 数据库最新的对象
    private Object busEntity;

    // 前端对象
    private Object entityWeb;

    /** 审批意见 */
    private String comment;

    /** 审批所上传的签名 64  */
    private String signBase64;

    /** 附件地址 */
    private String attachmentUrl;

    private Object handledResult;

    private String handledMsg;

    public String getFlowKey() {
        return flow != null ? flow.getCode() : null;
    }

    public String getButtonKey() {
        return button != null ? button.getCode() : null;
    }

    /**
     * 是否起始节点
     * @return
     */
    public boolean isStartNode() {
        return button.getFlagStart();
    }

    /**
     * 是否结束节点
     * @return
     */
    public boolean isEndNode() {
        return button.getFlagEnd();
    }

    /**
     * 是否作废节点
     * @return
     */
    public boolean isCloseNode() {
        return button.getFlagClose() != null ? button.getFlagClose() : false;
    }

    /**
     * 获取当前业务实体，只在service 方法中有效
     * @param <T>
     * @return
     */
    public <T> T getBusEntity(){
        return (T) this.busEntity;
    }
    /**
     * 获取当前业务实体，只在service 方法中有效
     * @param <T>
     * @return
     */
    public void addBusIdError(String busId,String busKey,String errMsg){
        if (this.getBusIdErrorList() == null) {
            this.busIdErrorList = new ArrayList<>();
        }

        if (this.getBusErrorMsgList() == null) {
            this.busErrorMsgList = new ArrayList<>();
        }
        if (this.getBusKeyErrorList() == null) {
            this.busKeyErrorList = new ArrayList<>();
        }
        this.busIdErrorList.add(busId);
        this.busKeyErrorList.add(busKey);
        this.busErrorMsgList.add(errMsg);
    }
}
